<?php
include_once('simple_html_dom.php');
include_once ('common.php');
class Kqxsday {

	private $crawlUrl;
	private $xpath='html body form#aspnetForm div.bg_body div.conten div.kqxs div.boder div div table tr td table';
	private $tittleXpath='span#Ldate';
	public function __construct( $url ) {
		$this->crawlUrl=$url;
	}

	// Get data for comparison only
	function crawl() {

		$result = $this->rawCrawl();
		$rawDetail=$result['rawdetail'];
		$detail=array();

		// Other prizes
		foreach($rawDetail as $item){
			$prizes=explode('-', $item);
			$prizes=array_map('trim', $prizes);
			$detail=array_merge($detail,$prizes);
		}

		$result['detail']=$detail;
		return $result;
	}

	function rawCrawl() {
		$result = array('open_date'=>'','rawdetail'=>array());
		$rawDetail=array();

		// get DOM from URL or file
		$context=stream_context_create(array('http'=>
				array(
		        'timeout' => 1200 // 20 minutes
				)
		));
		$html = file_get_html($this->crawlUrl,false,$context);

		// Get open_date
		$titleSpanList = $html->find($this->tittleXpath);
		$titleSpan=$titleSpanList[0];
		$title = trim($titleSpan->innertext);
		$title =str_replace('/','-',$title);
		$pattern='/\d\d-\d\d-\d\d\d\d/';
		preg_match($pattern, $title, $matches, PREG_OFFSET_CAPTURE);

		$result['open_date']=$matches[0][0];
		$result['open_date']=nomalizeDate($result['open_date']);

		$mainTableList =$html->find($this->xpath);
		$mainTable=$mainTableList[0];

		$rowList =$mainTable->children();

		foreach($rowList as $row)
		{
			$colList = $row->children();
			$td=$colList[1];
			$prizes=explode('-', $td->text());
			$prizes=array_map('trim', $prizes);
			$rawDetail=array_merge($rawDetail,$prizes);
		}

		$rawDetail =array_reverse ($rawDetail);
		$result['rawdetail']=$rawDetail;

		return $result;
	}
}

?>